<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="商品分类" field="title">
            <a-tree-select
              v-model="searchForm.category_id"
              :data="optionData.category_id"
              :field-names="{ key: 'id', title: 'category_name', icon: 'customIcon' }"
              placeholder="请选择商品分类"
              allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="商品类型" field="goods_type">
            <sa-select v-model="searchForm.goods_type" dict="app_goods_type" placeholder="请选择商品类型" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="商品名称" field="goods_name">
            <a-input v-model="searchForm.goods_name" placeholder="请输入商品名称" allow-clear />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #status="{ record }">
        <sa-switch v-model="record.status" @change="changeStatus($event, record.id)"></sa-switch>
      </template>
      <template #goods_name="{ record }">
        <span class="text-wrap line-clamp-2 text-xs">{{ record.goods_name }}</span>
      </template>
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import commonApi from '@/api/common'
import EditForm from './edit.vue'
import api from '../../api/goods/goods'

// 引用定义
const crudRef = ref()
const editRef = ref()
const optionData = reactive({
  category_id: [],
})

// 搜索表单
const searchForm = ref({
  category_id: '',
  goods_type: '',
  goods_name: '',
})

const changeStatus = async (status, id) => {
  const response = await api.changeStatus({ id, status })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/app/saiuser/goods/AppGoods/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {
    show: true,
    auth: ['/app/saiuser/goods/AppGoods/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/app/saiuser/goods/AppGoods/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: '商品ID', dataIndex: 'id', width: 100 },
  { title: '商品封面', dataIndex: 'cover_image', type: 'image', size: 48, width: 100 },
  { title: '商品名称', dataIndex: 'goods_name', width: 250 },
  { title: '商品类型', dataIndex: 'goods_type', type: 'dict', dict: 'app_goods_type', width: 120 },
  { title: '商品轮播', dataIndex: 'banner', type: 'image', size: 48, width: 120 },
  { title: '商品价格', dataIndex: 'goods_price', width: 100 },
  { title: '划线价格', dataIndex: 'line_price', width: 100 },
  { title: '库存总量', dataIndex: 'stock_total', width: 100 },
  { title: '初始销量', dataIndex: 'sales_initial', width: 100 },
  { title: '实际销量', dataIndex: 'sales_actual', width: 100 },
  { title: '商品状态', dataIndex: 'status', type: 'dict', dict: 'data_status', width: 100 },
  { title: '排序', dataIndex: 'sort', width: 100 },
])

// 页面数据初始化
const initPage = async () => {
  const resp_category_id = await commonApi.commonGet('/app/saiuser/goods/AppGoodsCategory/index')
  optionData.category_id = resp_category_id.data
}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
